package com.ldg.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ldg.page.entity.GoodsDescEntity;
import com.ldg.page.entity.GoodsEntity;
import com.ldg.page.entity.ItemEntity;
import com.ldg.page.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Liudao
 * @Date: 2022-09-28-19:24
 * @Description:
 */
@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private GoodsDescService goodsDescService;
    @Autowired
    private ItemService itemService;

    /**
     * 根据商品id创建静态页面
     * @param id
     */
    @Override
    public void creatP(long id) throws IOException {
        //1.1 定义当前模板引擎的上下文对象
        Context context = new Context();
        //1.2 定义数据map
        Map<String, Object> dataMap = new HashMap<>();
        //1.2.1 放spu商品到map中
        //1.2.1.1 查询spu商品信息
        GoodsEntity goods = goodsService.getById(id);
        //1.2.1.2 将商品放到map中
        dataMap.put("goods",goods);

        //1.2.2 查找一、二、三级分类
        //1.2.2.1 查找出一级分类
        String category1Name = itemCatService.getById(goods.getCategory1Id()).getName();
        String category2Name = itemCatService.getById(goods.getCategory2Id()).getName();
        String category3Name = itemCatService.getById(goods.getCategory3Id()).getName();
        //1.2.2.2 放三级分类到map中
        dataMap.put("category1Name",category1Name);
        dataMap.put("category2Name",category2Name);
        dataMap.put("category3Name",category3Name);

        //1.2.3 查询goodsDesc
        //1.2.3.1 查询spu商品描述表的信息
        GoodsDescEntity goodsDesc = goodsDescService.getById(id);
        //1.2.3.2 将它放到map中
        dataMap.put("goodsDesc",goodsDesc);
        //1.2.3.3 将itemImages转换为java集合
        List<Map> itemImages = JSON.parseArray(goodsDesc.getItemImages(), Map.class);
        //1.2.3.4 放到map中
        dataMap.put("itemImages",itemImages);
        //1.2.3.5 将customAttributeItems转换为java集合
        List<Map> customAttributeItems = JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class);
        //1.2.3.6 放到map中
        dataMap.put("customAttributeItems",customAttributeItems);
        //1.2.3.7 将specificationItems转换为java集合
        List<Map> specificationItems = JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class);
        //1.2.3.8 放到map中
        dataMap.put("specificationItems",specificationItems);
        //1.2.3.9 查询sku商品列表
        List<ItemEntity> items = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id).orderByDesc("is_default"));
        //1.2.3.10 放到dataMap中
        dataMap.put("items",items);
        //1.2.3.11 取出默认值为1的哪件商品
        dataMap.put("sku",items.get(0));


        //1.3 当上下文对象绑定到数据集合
        context.setVariables(dataMap);

        //1.4 定义输出流对象
        Writer writer = new FileWriter("D:\\JavaGenerator\\html" + id + ".html");
        //1.5 进行模板引擎的处理（生成页面）
        templateEngine.process("item",context,writer);
    }
}
